package controlGraph;

/**
 * @author Administrator
 * This Object is used to store the ArrayRef variable inside Axiom Sets
 */
public class ArrayRef extends LValue{
	
	private Expression[] expr; 
	
	public ArrayRef(Expression [] temp){
		this.expr = temp;
	}

	@Override
	public String toString() {
		String ret = "";
		for(int i=0;i<expr.length;i++){
			ret= ret+expr[i].toString();
		}
		return ret;
	}
	
	public String getVariable(){
		
		if(!(this.expr[0] instanceof Variable)){
			System.out.println("ERROR : The first argument should be a variable. But here it is not");
			System.exit(1);
		}
		Variable temp = (Variable) this.expr[0];
		return temp.getVariable();
	}
	
	public Expression getVariableExpression(){
		return this.expr[0];
	}
	
	public Expression getIndexExpression(){
		return this.expr[1];
	}
	
	public boolean isSame(Expression e){
		if(e instanceof ArrayRef){
			ArrayRef temp = (ArrayRef)e;
			
			Variable one = (Variable)temp.getVariableExpression();			
			boolean should1 = one.isSame(this.expr[0]);
			boolean should2 = false;
			
			Expression index = temp.getIndexExpression();
			if(index instanceof LValueInt){
				LValueInt two = (LValueInt)temp.getIndexExpression();
				should2 = two.isSame(this.expr[1]);
			}
			
			if(index instanceof Variable){
				Variable two = (Variable)temp.getIndexExpression();
				should2 = two.isSame(this.expr[1]);
			}
			
			if(index instanceof ArrayRef){
				ArrayRef two = (ArrayRef)temp.getIndexExpression();
				should2 = two.isSame(this.expr[1]);
			}
			
			if(should1 && should2)
				return true;
		}
		
		return false;
	}
	
	public boolean equals(Object e){
		if(e instanceof ArrayRef){
			ArrayRef temp = (ArrayRef)e;
			
			Variable one = (Variable)temp.getVariableExpression();			
			boolean should1 = one.equals(this.expr[0]);
			boolean should2 = false;
			
			Expression index = temp.getIndexExpression();
			if(index instanceof LValueInt){
				LValueInt two = (LValueInt)temp.getIndexExpression();
				should2 = two.equals(this.expr[1]);
			}
			
			if(index instanceof Variable){
				Variable two = (Variable)temp.getIndexExpression();
				should2 = two.equals(this.expr[1]);
			}
			
			if(index instanceof ArrayRef){
				ArrayRef two = (ArrayRef)temp.getIndexExpression();
				should2 = two.equals(this.expr[1]);
			}
			
			if(should1 && should2)
				return true;
		}
		
		return false;
	}
	
	public int hashCode(){
		Variable one = (Variable)this.getVariableExpression();
		int hash = one.hashCode();
		
		Expression index = this.getIndexExpression();
		if(index instanceof LValueInt){
			LValueInt two = (LValueInt)index;
			hash = hash * two.hashCode();
		}
		
		if(index instanceof Variable){
			Variable two = (Variable)index;
			hash = hash * two.hashCode();
		}
		return hash;
	}
}
